package scalaLearning.part1

import org.junit.Test

import scala.collection.mutable.ArrayBuffer


class BasicTests {


  //不可变长数组
  @Test
  def ArrayTest(): Unit = {
    val arr1 = Array(1, 2, 3, 4, 5)
    val arr2 = Array[Int](1, 2, 3, 4, 5)
    val arr3 = Array("a", 2, 3, 4, 5)
    val arr4 = Array[Any]("a", 2, 3, 4, 5)
    //arr5是一个长度为5的Array
    val arr5 = new Array[Int](5)
    //arr6是一个值为5的Array(长度为1)
    val arr6 = Array[Int](5)
    //Array的长度是不能改变的，但是可以改变数组中的值
    arr1.foreach(println)
    arr1(0) = 10
    println("----------")
    arr1.foreach(println)
  }

  //可变长数组
  @Test
  def ArrayBufferTest(): Unit = {
    val ab = new ArrayBuffer[Int]()
    ab += 1
    ab += 3
    ab.append(5)
    ab -= 1
    //两个数组的合并
    val aa: ArrayBuffer[Int] = ArrayBuffer(7, 9)
    ab ++= aa
    ab.foreach(println)
  }

  @Test
  def MethodsTest(): Unit = {
    val ab: ArrayBuffer[Int] = ArrayBuffer(1, 3, 5)
    //插入元素
    // 第一个代表插入的位置，后面的为插入的元素
    ab.insert(0, -1, -2)
    ab.foreach(println)
    println("-------")
    //删除元素
    // 第一个代表删除的位置，第二个代表删除的个数。
    ab.remove(0, 2)
    ab.foreach(println)
    println("-------")
    //不可变长转化为可变长
    val arr: Array[Int] = Array(1, 2, 3, 4)
    println(arr.toBuffer)
    //聚合操作
    println(arr.max)
    println(arr.min)
    println(arr.sum)
    println("-------")
    //将元素按照指定符号拼接起来
    println(arr.mkString("-"))
    val arr2: Array[Int] = Array(1, 2, 3, 1)
    println(arr2.sorted)
    println(arr2.reverse)
    println("-------")

    //    arr.sortWith(_>_) 降序 arr.sortWith((x,y)=>x>y)
    //    arr.sortWith(_<_) 升序 arr.sortWith((x,y)=>x<y)
    //    arr.sortBy(x => x.toString)
    //    arr.sortBy(x => x+””)
    //    arr.sortBy(x => x) 升序
    //      arr.sortBy(x => -x) 降序
  }

  @Test
  def Test1(): Unit = {
    println("hello scala")
    //        for循环
    for (i <- 1 to 10) print(i + " ")
    for (i <- 1 until 10) print(i + " ")
    for (i <- 1 to 3 if i != 2) print(i + " ")
    for (i <- 1 to 3; j = 4 - i) print(j + " ")
    for (i <- 1 to 3; j <- 1 to 3) println("i=" + i + " j=" + j)

    //        循环返回值
    val res = for (i <- 1 to 10) yield {
      if (i % 2 == 0) {
        i
      } else {
        "不是偶数"
      }
    }
    println(res)
  }

  @Test
  def Test2(): Unit = {
    //    方法的定义
    //    def m(x: Int, y: Int): Int = {
    //      x + y
    //    }
    //    println(m(10,10))

    def sum(nums: Int*) = {
      var res = 0
      for (num <- nums)
        res += num
      res
    }

    println("sum=" + sum(1, 2, 3, 4, 5))

  }

  @Test
  def Test3(): Unit = {
    //函数的定义
    //    val f1=(x:Int,y:Int) => x+y
    //    println(f1(10,10))
    val arr = Array(1, 2, 3, 4, 5, 6)
    println(arr.map(x => x * x).toBuffer)
    println(arr.map(_ * 10).toBuffer)
  }

  @Test
  def Test4(): Unit = {
    println("abc\ru\ra")
  }

  //scala输出的三种方式
  @Test
  def Test5(): Unit = {
    val name: String = "Tom"
    val sal: Double = 1.2
    println("hello" + sal + name)
    printf("name=%s sal=%f\n", name, sal)
    println(s"第三种方式 name=$name sal=${sal + 1}")
  }

  @Test
  def Test6(): Unit = {
    val arr1: Array[Int] = Array(1, 2, 2, 3, 4)
    arr1.foreach(println)

  }

  @Test
  def Test7(): Unit = {
    def sum(nums: Int*) = {
      var result = 0
      for (num <- nums) {
        result += num
      }
      result
    }

    println(sum(1, 2, 3, 4, 5))
    val arr: Array[Int] = Array(1, 2, 3)
    println(sum(arr: _*))
  }

  @Test
  def Test8(): Unit = {
    val arr2: Array[Int] = Array(1, 2, 3, 4, 5, 6)
    println(arr2.toBuffer)
    println(arr2)
  }

  @Test
  def Test9(): Unit ={
    def func(nums:Int*)={
      nums.sum
    }
    println(func(1,2,3))
  }

  @Test
  def Test10(): Unit ={
    val arr1: Array[String] = Array("a", "b")
    val arr2: Array[String] = Array("A", "B", "C")
    val arr3: Array[(String, String)] = arr2.zip(arr1)
    arr3.foreach(println)
  }

  @Test
  def Test():Unit={
    val a=1
    val res=if(a==1)"error" else 1
    println(res.getClass)
  }
  //
}
